package cn.wolfcode.web.controller;


import cn.wolfcode.common.constants.CommonConstants;
import cn.wolfcode.common.domain.UserInfo;
import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.CodeMsg;
import cn.wolfcode.common.web.CommonCodeMsg;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.common.web.anno.RequireLogin;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.domain.PayVo;
import cn.wolfcode.redis.CommonRedisKey;
import cn.wolfcode.service.IOrderInfoService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;


@RestController
@RequestMapping("/orderPay")
@RefreshScope
@Slf4j
public class OrderPayController {
    private final IOrderInfoService orderInfoService;

    private final StringRedisTemplate redisTemplate;

    public OrderPayController(IOrderInfoService orderInfoService, StringRedisTemplate redisTemplate) {
        this.orderInfoService = orderInfoService;
        this.redisTemplate = redisTemplate;
    }


    @RequestMapping("/refund")
    @RequireLogin
    public Result<?> refund(String orderNo, @RequestHeader(CommonConstants.TOKEN_NAME) String token){
        UserInfo userInfo = getUserByToken(token);
        orderInfoService.doRefund(orderNo, userInfo.getPhone());
        return Result.success("退款成功");
    }

    @RequestMapping("/integral")
    @RequireLogin
    public Result<?> integral(String orderNo, @RequestHeader(CommonConstants.TOKEN_NAME) String token){
        UserInfo userInfo = getUserByToken(token);
        orderInfoService.integralPay(orderNo, userInfo.getPhone());
        return Result.success("积分支付成功");
    }

    @RequestMapping("/findPayVoByOrderNo")
    public Result<PayVo> findPayVoByOrderNo(String orderNo, @RequestHeader(CommonConstants.TOKEN_NAME) String token){
        UserInfo userInfo = getUserByToken(token);
        if (userInfo == null) {
            return Result.error(CommonCodeMsg.ILLEGAL_OPERATION);
        }
        OrderInfo orderInfo = orderInfoService.selectByOrderNo(orderNo, userInfo.getPhone());
        if (! orderInfo.getStatus().equals(OrderInfo.STATUS_ARREARAGE)) {
            log.error("[支付宝支付] 订单支付失败, 订单状态: {}, 订单号:{}",orderInfo.getStatus(),orderNo);
            throw new BusinessException(new CodeMsg(5009600, "请确定订单未支付"));
        }
        PayVo payVo = new PayVo();
        payVo.setOutTradeNo(orderInfo.getOrderNo());
        payVo.setSubject(orderInfo.getProductName());
        payVo.setTotalAmount(orderInfo.getSeckillPrice().toString());
        payVo.setBody("[秒杀商品] " + orderInfo.getProductName());
        return Result.success(payVo);
    }

    @RequestMapping("/paySuccess")
    public Result<?> paySuccess(String outTradeNo, String tradeNo, String totalAmount){
        orderInfoService.paySuccess(outTradeNo, tradeNo, totalAmount);
        return Result.success(null);
    }

    private UserInfo getUserByToken(String token) {
        return JSON.parseObject(redisTemplate.opsForValue().get(CommonRedisKey.USER_TOKEN.getRealKey(token)), UserInfo.class);
    }

}
